﻿package com.thomasbrault.core
{
	import flash.text.TextField;	

	/**
	 * @author thomas
	 * update: 
	 * get translation by object is more faster (X10) than by a XML request
	 */
	public class Translations 
	{

		private static var _translation : Array;
		
		/**
		* set XML translation to the class .
		* XML Stucture:
		* <root>
			<group name="groupe-name">
				<field name="rules" type="text">
					<![CDATA[values text]]>
				</field>
				<field name="terms" type="html">
					<![CDATA[values html]]>
				</field>
			</group>
		</root>
		*/
		public static function setDatas ( datas : XML ) : void
		{
			_translation = new Array();
			for each (var node : XML in datas.elements())
			{
				_translation[node.@name] = new Array();
				for each (var node2 : XML in node.children())
					_translation[node.@name][node2.@name] = new Translation(node2,node2.@type,node2.@font);
			}
			
		}
		
				
		/**
		* get a translation from the XML translation
		* Warning : setDatas must be called before 
		* usage: 
		* 	myTextfield.text = Translations.getTranslation('group-name','rules');
		*/
		public static function getTranslation ( group : String , field : String ) : String
		{
			var s : String;
			
			if ( Translations.check(group, field) != null )
				s =  Translations.check(group, field) ;
			
			s = getObject(group,field).value;
			
			return s;
		}
		
		
		/**
		*
		*/
		public static function getNumTranslation ( group : String ) : int
		{
			var count : int = 0;
			for ( var i in _translation[group] )
				count++;
			
			return count;
		}
		
		/**
		* get a translation object from the XML translation (value and type)
		* Warning : setDatas must be called before 
		* usage: 
		* 	myTextfield.text = Translations.getTranslation('group-name','rules').value;
		*/
		public static function getObject ( group : String , field : String ) : Translation
		{
			if ( Translations.check(group, field) != null )
				return Translations.check(group, field) ;
			
			return _translation[group][field];
		}
		
		
		
		/**
		* set a textfield with the translation value, with the proper propertie (htmlText or text)
		* Warning : setDatas must be called before 
		* usage: 
		* 	Translations.setText('group-name','rules');
		*/
		public static function setText (tf : TextField, group : String, field : String ) : void
		{
			if ( Translations.check(group, field) != null )
				tf.text = Translations.check(group, field);
			
			if ( getObject(group,field).type == 'html' )
				tf.htmlText = getObject(group,field).value;
			else 
				tf.text 	= getObject(group,field).value;
		}
		
		
		public static function autoFill ( tf : TextField ) : void
		{
			var auto 		: String = String(tf.text).replace("\r", "");
			var splited 	: Array = auto.split(','); 
			setText(tf, splited[0], splited[1]);
		}
		
		/**
		* verify if a translation exist
		*/
		public static function check ( group : String, field : String ) : *
		{
			
			if ( _translation == null ){
				throw (new Error('Translation Error : Translation XML not set'));
				return '/!\\ Translation XML not set';
			}
			
			if ( _translation[group] == null ){
				throw (new Error('Translation Error : group "'+group+'" not exist'));
				return '/!\\ group : '+group+' not exist';
			}
			
			if ( _translation[group][field] == null ){
				throw (new Error('Translation Error : field "'+field+'" not exist'));
				return '/!\\ field : '+field+' not exist';
			}
			
			
			return null;
			
		}
	}

	
}

class Translation extends Object 
{
	public var value : String;
	public var type : String;
	public var font : String;
	
	function Translation ( $value : String, $type : String, $font : String = '' ) 
	{
		value = $value;
		type = $type;
		font = $font;
	}
}
